package com.yq.service.impl;

import com.yq.mapper.AdminMapper;
import com.yq.model.bo.*;
import com.yq.model.po.Goods;
import com.yq.model.po.Spec;
import com.yq.model.vo.AdminGoodsTypeVO;
import com.yq.model.vo.GetGoodsByIdVO;
import com.yq.model.vo.GoodsVO;
import com.yq.model.vo.NoReplyMsgVO;
import com.yq.model.vo.entity.SpecsEntity;
import com.yq.service.AdminGoodsService;
import com.yq.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class AdminGoodsServiceImpl implements AdminGoodsService {
    @Override
    public List<AdminGoodsTypeVO> getGoodsType() {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        List<AdminGoodsTypeVO> list  =  mapper.getGoodsType();
        sqlSession.commit();
        sqlSession.close();
        return list;
    }

    @Override
    public int addGoods(AdminAddGoodsBO adminAddGoodsBO) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        //逻辑 分别掺入两张表 goods spec
        try {
            List<SpecListEntity> specList = adminAddGoodsBO.getSpecList();
            int stocknum =0;
            double price = Double.MAX_VALUE;
            for (SpecListEntity specListEntity : specList) {
                if (specListEntity.getUnitPrice() < price){
                    price = specListEntity.getUnitPrice();

                }
                stocknum+=specListEntity.getStockNum();
            }
            Goods goods = new Goods(null,adminAddGoodsBO.getName(),adminAddGoodsBO.getTypeId(),
                    adminAddGoodsBO.getImg(),adminAddGoodsBO.getDesc(),
                    price,stocknum);
            mapper.AddGoods(goods);
            //插入规格
            ArrayList<Spec> specsList = new ArrayList<>();
            Integer goodsid = goods.getId();
            for (SpecListEntity specListEntity : specList) {
                specsList.add(new Spec(null,specListEntity.getSpecName(),goodsid,
                        specListEntity.getUnitPrice(),specListEntity.getStockNum()));
            }
            mapper.addSpecs(specsList);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return 404;
        }finally {
            sqlSession.close();
        }


        return 200;
    }

    @Override
    public List<GoodsVO> searchGoodsByTypeIdInfo(int typeIds) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        List<GoodsVO> list  = mapper.searchGoodsByTypeIdInfo(typeIds);
        sqlSession.commit();
        sqlSession.close();

        return list;
    }

    @Override
    public void deleteGoodsById(Integer goodsId) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        try {
            mapper.deleteSpecById(goodsId);
            mapper.deleteGoodsById(goodsId);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            sqlSession.close();
        }

    }

    @Override
    public List<GetGoodsByIdVO> getGoodsInfoById(int goodsId) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        List<GetGoodsByIdVO> getGoodsByIdVO=null;
        try {
             getGoodsByIdVO =mapper.getGoodsInfoById(goodsId);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
        }finally {
            sqlSession.close();
        }
        return getGoodsByIdVO;
    }

    @Override
    public int addGoodsType(AddTypeBO addTypeBO) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        int count=0;
        try {
            count = mapper.addGoodsType(addTypeBO);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            sqlSession.close();
        }
        if (count==1){
            return 200;
        }
        return 404;
    }

    @Override
    public int deleteGoodsTypeById(int tId) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        int count=0;
        try {
            count = mapper.deleteGoodsTypeById(tId);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            sqlSession.close();
        }
        if (count==1){
            return 200;
        }
        return 404;
    }

    @Override
    public int updateGoods(UpdateGoodsBO updateGoodsBO) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        int count=0;
        try {
            ///首先修改商品表
            List<SpecsEntity> specList = updateGoodsBO.getSpecList();
            int stocknum =0;
            double price = Double.MAX_VALUE;
            for (SpecsEntity specEntity : specList) {
                if (specEntity.getUnitPrice() < price){
                    price = specEntity.getUnitPrice();
                }
                stocknum+=specEntity.getStockNum();
            }
            Goods goods = new Goods(updateGoodsBO.getId(),updateGoodsBO.getName(),updateGoodsBO.getTypeId(),
                    updateGoodsBO.getImg(),updateGoodsBO.getDesc(), price,stocknum);
            mapper.updateGoods(goods);

            //修改规格表  方式1，先删除所有的，再重新的添加

            //1 删除和该商品有关的数据，再添加所有的
            Integer goodsid = goods.getId();

            mapper.deleteSpec(goodsid);
            // 2 加入所有的
            ArrayList<Spec> specsList = new ArrayList<>();
            for (SpecsEntity SpecsEntity : specList) {
                specsList.add(new Spec(SpecsEntity.getId(),SpecsEntity.getSpecName(),goodsid,
                        SpecsEntity.getUnitPrice(),SpecsEntity.getStockNum()));
            }
//            for (Spec spec : specsList) {
//                mapper.updateGoodsType(spec);
//            }
            mapper.addSpecs(specsList);
            sqlSession.commit();
            return 200;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            sqlSession.close();
        }
        return 404;

    }

    @Override
    public List<NoReplyMsgVO> getNoReplyMsg(int state) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        List<NoReplyMsgVO> list =  mapper.getNoReplyMsg(state);
        sqlSession.commit();
        sqlSession.close();
        return list;
    }

    @Override
    public List<NoReplyMsgVO> repliedMsg(int state) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        List<NoReplyMsgVO> list =  mapper.getNoReplyMsg(state);
        sqlSession.commit();
        sqlSession.close();
        return list;
    }

    @Override
    public int replay(ReplyBO replyBO) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        int count =0;
        try {
             count = mapper.replay(replyBO);
            sqlSession.commit();

        }catch (Exception e){
            sqlSession.rollback();
        }finally {
            sqlSession.close();
        }
        if (count==1){
            return 200;
        }
        return 404;
    }
}
